﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Testimonial_TestimonialAdd : System.Web.UI.UserControl
{
    private int nDefaulLang = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
        if (!IsPostBack)
        {
            setLable();
            SetValidate();
            InitData();
        }
    }
    private void InitData()
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                {
                    GetData(nItemID);
                }
            }
            else
            {
                imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
            }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }
    private void GetData(int ItemID)
    {
        int nStatus = 0;
        Testimonial objTestimonial = new Testimonial();
        objTestimonial.LoadById(ItemID);

        txtTitle.Text = objTestimonial.Data.Title;
        FTBContent.IntialText(ConvertData.ConvertToString(objTestimonial.Data.TestimonialContent));

        nStatus = ConvertData.ConvertToInt(objTestimonial.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

        string sImage = objTestimonial.Data.Image;
        if (sImage.Length > 0)
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + sImage;//testimonial
        else imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
    }
    private eTestimonial SetData()
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        eTestimonial eTestimonialEntity = this.TestimonialEntity();
        Testimonial objTestimonial = new Testimonial();

        string sImage = "";
        if (iptImage.Value != "")
        {
            Support.DeleteFile("testimonial", eTestimonialEntity.Image);
            sImage = Support.UploadFile(iptImage, "testimonial");
        }
        else
        {
            if (sQueery == Config.ACTION_EDIT)
                sImage = eTestimonialEntity.Image;
        }
        objTestimonial.Data.Image = sImage;

        if (chkActive.Checked) objTestimonial.Data.Status = Config.STATUS_ACTIVE;
        else objTestimonial.Data.Status = Config.STATUS_INACTIVE;

        objTestimonial.Data.TestimonialContent = ConvertData.ConvertToString(FTBContent.RenderText());
        objTestimonial.Data.Title = ConvertData.ConvertToString(txtTitle.Text);

        return objTestimonial.Data;
    }
    private eTestimonial TestimonialEntity()
    {
        eTestimonial entityTestimonial = new eTestimonial();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Testimonial objTestimonial = new Testimonial();
            objTestimonial.LoadById(nItemID);
            entityTestimonial = objTestimonial.Data;
        }
        return entityTestimonial;
    }
    private void setLable()
    {
        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaulLang - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaulLang - 1];
        lblTitle.Text = MultipleLanguage.LBL_TITLE[nDefaulLang - 1];
        lblContent.Text = MultipleLanguage.LBL_CONTENT[nDefaulLang - 1];
        lblAvatar.Text = MultipleLanguage.LBL_IMAGE[nDefaulLang - 1];
        chkActive.Text = MultipleLanguage.LBL_ACTIVE[nDefaulLang - 1];
    }
    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.TESTIMONIAL_TITLE_EMPTY[nDefaulLang - 1];
    }
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sAction = GetQuerryString(Config.ACTION);
        int nActionResult = 0;
        int nDefaultLanguage = Support.GetDefaultLanguage();
        Testimonial objTestimonial = new Testimonial();
        objTestimonial.Data = this.SetData();

        if (sAction == Config.ACTION_EDIT)
        {
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objTestimonial.Update(nItemID);//Update
            
            if (nActionResult> 0)//Kiem tra co loi xay ra
            {
                MessAlert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]);
                if (objTestimonial.Data.Image.ToString() != "")
                {
                    imgImage.ImageUrl = Config.ROOT + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD + objTestimonial.Data.Image.ToString();
                }
                if (objTestimonial.Data.Image.ToString() == "")
                {
                    objTestimonial.Data.Image = Config.ROOT + "/Images/no_image.gif";
                }
            }
            else
                MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]);
        }
        else
        {
            nActionResult = objTestimonial.Insert();
            if (nActionResult > 0)
            {
                
                    string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                    Response.Redirect(sURL);
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL;
        Response.Redirect(sRedirect);
    }
}